package com.cw.jeeyt.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemTemplateInfoBo;
import com.cw.jeeyt.service.bo.system.SystemTemplateModuleInfoBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.system.SystemTemplateInfoService;
import com.cw.jeeyt.service.intf.system.SystemTemplateModuleInfoService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 生成代码模板信息控制层
 *
  * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/systemTemplateInfo")
public class SystemTemplateInfoController extends BaseController {
    @Resource
    private SystemTemplateInfoService systemTemplateInfoService;
    @Resource
    private SystemTemplateModuleInfoService systemTemplateModuleInfoService;

    /**
     * 跳转到生成代码模板信息列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() {
        ModelAndView mv = new ModelAndView("system/systemTemplateInfo_index");
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listSystemTemplateInfo")
    public ResultData listSystemTemplateInfo(@RequestBody DataTable dataTable) throws Exception {

        SystemTemplateInfoBo systemTemplateInfoBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemTemplateInfoBo.class);
        if (systemTemplateInfoBo == null) {
            systemTemplateInfoBo = new SystemTemplateInfoBo();
        }
        PageInfo<SystemTemplateInfoBo> pageInfo = systemTemplateInfoService.queryByPage(systemTemplateInfoBo,
            dataTable.getStart(), dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到生成代码模板信息表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editForm(String id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("system/systemTemplateInfo_form");
        SystemTemplateInfoBo systemTemplateInfoBo = null;
        List<SystemTemplateModuleInfoBo> stmiList = new ArrayList<SystemTemplateModuleInfoBo>();

        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemTemplateInfoBo = new SystemTemplateInfoBo();
        } else {
            systemTemplateInfoBo = systemTemplateInfoService.getSystemTemplateInfoById(id);
            stmiList = systemTemplateModuleInfoService.queryListByTemplateId(id);
        }
        mv.addObject("systemTemplateInfo", systemTemplateInfoBo);
        mv.addObject("systemTemplateModuleInfoList", stmiList);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }
    /**
     * 跳转到生成代码模板信息列表查询页面
     *
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/to-cloneTemplate")
    public ModelAndView toCloneTemplate(String templateId) {
        ModelAndView mv = new ModelAndView("system/systemTemplateInfo_cloneTemplate");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("templateId",templateId);
        return mv;
    }
    /**
     * 根据操作类型保存生成代码模板信息数据
     *
     * @param systemTemplateInfoBo
     * @param operation add 新增  edit 修改
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemTemplateInfo", method = RequestMethod.POST)
    public JsonResultDto saveSystemTemplateInfo(SystemTemplateInfoBo systemTemplateInfoBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                Map<String, Object> map = new HashMap<>();
                String id = systemTemplateInfoService.insert(systemTemplateInfoBo);
                map.put("id", id);
                jsonResultDto.setResultData(map);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemTemplateInfoService.update(systemTemplateInfoBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("保存失败:" + e.getMessage());

        }
        return jsonResultDto;
    }

    /**
     * 删除生成代码模板信息
     *
     * @param systemTemplateInfoBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(SystemTemplateInfoBo systemTemplateInfoBo) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemTemplateInfoService.delete(systemTemplateInfoBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("删除失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
    /**
     * 初始化生成代码模板文件，如果有调整，则只需点击初始化文件就可以同步
     *
     * @param systemTemplateInfoBo
     */
    @RequestMapping(value = "/initTemplateFile")
    @ResponseBody
    public JsonResultDto initTemplateFile(SystemTemplateInfoBo systemTemplateInfoBo) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemTemplateInfoService.initTemplateFile(systemTemplateInfoBo);
            jsonResultDto.setSuccess(true);
            jsonResultDto.setMsg("初始化生成代码模板文件成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("初始化生成代码模板文件失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
    /**
     * 克隆模板信息
     *
     * @param systemTemplateInfoBo
     */
    @RequestMapping(value = "/cloneTemplate")
    @ResponseBody
    public JsonResultDto cloneTemplate(SystemTemplateInfoBo systemTemplateInfoBo , String templateId) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemTemplateInfoService.cloneTemplate(systemTemplateInfoBo,templateId);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("删除失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 跳转到选择生成代码模板页面
     *
     * @param returnMethod 返回方法
     * @param selectType 选择方式 multiple 多选  single 单选
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectSystemTemplateInfo")
    public ModelAndView selectSystemTemplateInfo(String returnMethod, String selectType) {

        ModelAndView mv = new ModelAndView("system/systemTemplateInfo_select");
        mv.addObject("returnMethod", returnMethod);
        mv.addObject("selectType", selectType);
        return mv;

    }
}
